package Pages;

import java.util.Date;
import java.util.HashMap;
import clientMain.*;

/**
 * Confirm Buy Item Page
 * @author Andrew Fox
 *
 */

public class ConfirmBuyPage extends Page {
	HashMap<String, String> data=new HashMap<String, String>();
	String userName;
	int sellerRating=0;


	public ConfirmBuyPage(Page page){
		//		super(page.url,page.lastURL,page.client);
		//		this.html=page.html;
		//		this.pageType=page.pageType;
		super(page.url,page.html,page.client,page.pageType);
		this.pageOpenTime=page.pageOpenTime;
		this.lastURL=page.lastURL;
	}

	/**
	 * Determines the next link to go to in the application
	 * @return the URL or HTML of the next link, depending on whether
	 * 		   the previous entry contained a form or not
	 */
	public StringBuffer makeDecision() throws Exception{
		StringBuffer nextPage=new StringBuffer();
		if(HTML4==true){
			int start=html.indexOf("<a href=\"")+"<a href=\"".length();
			int end=html.indexOf("\">",start);
			nextPage=runSettings.rootURL.append("/").append(html.substring(start,end));
		}

		// Think Time
		try{Thread.sleep(getThinkTime());}
		catch(InterruptedException e){
			e.printStackTrace();
		}

		return nextPage;
	}

	/**
	 * Determines the think time before going to the next page
	 * Deducts from the think time the amount of time since the 
	 * page opened until this calculation is performed
	 * @return Think time in ms
	 */
	public int getThinkTime(){
		int thinkTime=(int)expDist(initialThinkTime);
		thinkTime+=(int)expDist(3000);

		if (verbose)System.out.println("User: "+client.userInfo.username+" - Think Time: "+thinkTime+" ms");
		return (int) ((thinkTime-(new Date().getTime()-pageOpenTime))/runSettings.thinkTimeSpeedUpFactor);
	}
}